package com.hxzy.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.hxzy.action.vo.TeamVo;
import com.hxzy.entity.Team;
import com.hxzy.entity.User;
import com.hxzy.service.ServiceResult;
import com.hxzy.service.TeamService;
import com.hxzy.service.TeamUserService;
import com.opensymphony.xwork2.ActionContext;

public class TeamAction extends AbstractAction {

	private TeamVo team;
	private String introduction;
	private String email;
	private List<Team> teamList;
	private int count;
	private String team_id;
	private TeamService teamService;
    private TeamUserService teamUserService;
	private static final long serialVersionUID = 6924069633327660840L;
	
	public String team() throws Exception {
        //ServletActionContext.getResponse().setCharacterEncoding("utf-8");
       // ServletActionContext.getResponse().setContentType("application/json");
	    if(isGet()){
	        return INPUT;
	    }
	    ServiceResult<Team> result=null;
	    if(team.getId()!=null){
	        result=teamService.update(team.getId(), team.getName(), team.getDesc(), team.getIntro(),team.getSubdomain(), team.getStartTime(), team.getEndTime());
	    }{
	        result=teamService.create(team.getName(), team.getDesc(), team.getIntro(),team.getSubdomain(),team.getStartTime(), team.getEndTime() );
	    }
	    if(result.status!=1){
	       switch (result.status) {
        case -1:
            addFieldError("team.startTime", result.message);
            break;
        case -3:
            addFieldError("team.name", result.message);
            break;
        case -4:
            addFieldError("team.subdomain", result.message);
        
        }
	       return INPUT;
	    }
//		User sessionUser=(User) ActionContext.getContext().getSession().get("userLogin");
// 		if(!teamService.findTeamName(team.getName(),team.getSubdomain())){
//			teamService.create(team.getName(),team.getDesc(), team.getSubdomain(),team.getIntro(), team.getStartTime(), team.getEndTime());
		//	ServletActionContext.getResponse().getWriter().print("{\"status\":1,\"message\":\"创建团队成功\"}");
	    this.team.setId(result.data.getId());
			return SUCCESS;
		//}else{
		//	ServletActionContext.getResponse().getWriter().print("{\"status\":2,\"message\":\"创建团队失败，团队名或域名重复\"}");
		//    addActionError("创建团队失败");
		//	return INPUT;
	}
	
	public String detaile() {
	    if(team!=null&&team.getId()!=null){
	        Team t=teamService.read(team.getId()).data;
	        this.team.setName(t.getName());
            this.team.setDesc(t.getDesc());
            this.team.setSubdomain(t.getSubdomain());
            this.team.setStatus(t.getStatus());
            this.team.setIntro(t.getIntro());
            this.team.setStartTime(new Date(t.getGmtBegin() * 1000));
            this.team.setEndTime(new Date(t.getGmtBegin() * 1000));
	    }
        return INPUT;
	}
	
	public String invateMember() throws Exception{
	    User sessionUser=(User) ActionContext.getContext().getSession().get("userLogin");
        String teamId=(String) ActionContext.getContext().getSession().get("team_id");
		    int result=teamService.invite(sessionUser.getId(), Long.parseLong(teamId), email);
		    if(result==1){
		        Map<String,Object> sessionEmail=ActionContext.getContext().getSession();
		        sessionEmail.put("email", email);
		        return SUCCESS;
		    }
		    addActionError("邮箱不存在");
			return INPUT;
		
	}
	
	public String myTeam(){
		User sessionUser=(User) ActionContext.getContext().getSession().get("userLogin");
		//teamList=teamService.findMyTeam(sessionUser.getId());
		//count=teamService.count(sessionUser.getId(),5);
		//teamList=teamService.datePage(1, 5, sessionUser.getId());
		  teamList=teamService.myTeam(sessionUser.getId());
	        count=teamService.count(sessionUser.getId(), 5);
		return INPUT;
		
	}
	
	public TeamUserService getTeamUserService() {
        return teamUserService;
    }

    public void setTeamUserService(TeamUserService teamUserService) {
        this.teamUserService = teamUserService;
    }

    public String getTeam_id() {
        return team_id;
    }

    public void setTeam_id(String team_id) {
        Map<String,Object> session=ActionContext.getContext().getSession();
        session.put("team_id", team_id);
          this.team_id = team_id;
    }

    public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List<Team> getTeamList() {
		return teamList;
	}

	public void setTeamList(List<Team> teamList) {
		this.teamList = teamList;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String getIntroduction() {
		return introduction;
	}

	public void setIntroduction(String introduction) {
		this.introduction = introduction;
	}

    public TeamVo getTeam() {
        return team;
    }

    public void setTeam(TeamVo team) {
        this.team = team;
    }

    public TeamService getTeamService() {
        return teamService;
    }

    public void setTeamService(TeamService teamService) {
        this.teamService = teamService;
    }


	
	
}
